package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.AppUserEntity;
import org.example.system.entity.CollectionEntity;
import org.example.system.entity.MediaEntity;
import org.example.system.mapper.MediaMapper;
import org.example.system.service.MediaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;

/**
 * 资源服务实现
 */
@Service
public class MediaServiceImpl implements MediaService {
    @Autowired
    private MediaMapper mediaMapper;

    /**
     * 按条件分页查询
     * @param media
     * @return
     */
    @Override
    public IPage<MediaEntity> selectPage(MediaEntity media){
        MPJLambdaWrapper<MediaEntity> wrapper = new MPJLambdaWrapper<>();
        //条件
        if (StrUtil.isNotEmpty(media.getMediaTitle())){
            wrapper.like(CollectionEntity:: getMediaTitle,media.getMediaTitle());    //相似的合集标题（模糊查询）
        }
        if (StrUtil.isNotEmpty(media.getMediaAuthor())){
            wrapper.like(CollectionEntity:: getMediaAuthor,media.getMediaAuthor());    //相似的作者（模糊查询）
        }
        if (StrUtil.isNotEmpty(media.getNickname())){
            wrapper.like(AppUserEntity:: getNickname,media.getNickname());    //相似的上传者昵称（模糊查询）
        }
        if (StrUtil.isNotEmpty(media.getMediaName())){
            wrapper.like(MediaEntity:: getMediaName,media.getMediaName());    //相似的章节名（模糊查询）
        }
        if (media.getMediaType() != null){
            wrapper.eq(CollectionEntity:: getMediaType,media.getMediaType());    //类型
        }
        if (StrUtil.isNotEmpty(media.getDesc())){
            wrapper.orderByDesc(MediaEntity::getCreateTime);
        }
        if (media.getCollectionId() != null){
            wrapper.eq(MediaEntity:: getCollectionId,media.getCollectionId());
        }
        if (media.getId() != null){
            wrapper.eq(MediaEntity:: getId,media.getId());
        }
        wrapper.selectAll(MediaEntity.class);
        wrapper.leftJoin(AppUserEntity.class, AppUserEntity::getId, MediaEntity::getUploaderId);
        wrapper.leftJoin(CollectionEntity.class, CollectionEntity::getId, MediaEntity::getCollectionId);
        wrapper.selectAs(AppUserEntity::getNickname, "nickname");
        wrapper.selectAs(AppUserEntity::getAvatar, "avatar");
        wrapper.selectAs(CollectionEntity::getMediaTitle, "media_title");
        wrapper.selectAs(CollectionEntity::getMediaAuthor, "media_author");
        wrapper.selectAs(CollectionEntity::getCoverPath, "cover_path");
        wrapper.selectAs(CollectionEntity::getMediaInfo, "media_info");
        wrapper.selectAs(CollectionEntity::getMediaType, "media_type");

        IPage<MediaEntity> page = new Page<>();
        page.setCurrent(media.getPageNum());
        page.setSize(media.getPageSize());
        return mediaMapper.selectPage(page,wrapper);
    }

    /**
     * 添加资源
     * @param media
     * @return
     */
    @Override
    public int add(MediaEntity media) {
        media.setCreateTime(new Date());
        return mediaMapper.insert(media);
    }

    /**
     * 修改
     *
     * @param media
     * @return
     */
    @Override
    public int update(MediaEntity media) {
        media.setUpdateTime(new Date());
        return mediaMapper.updateById(media);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return mediaMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     *
     * @param id
     * @return
     */
    @Override
    public MediaEntity getOne(Long id) {
        return mediaMapper.selectById(id);
    }
}
